﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;
namespace AnyOffice.general.sms
{
    public partial class query : ModulePage
    {
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            SmsDao dao = new SmsDao();
            foreach (RepeaterItem item in this.rptSms.Items)
            {
                HtmlInputCheckBox box = (HtmlInputCheckBox)item.FindControl("SmsId");
                if ((box != null) && box.Checked)
                {
                    dao.Delete(base.Conn, int.Parse(box.Value), base.User.Identity.Name);
                }
            }
            this.SmsQuery();
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            this.litNavTitle.Text = "短信查询结果";
            this.pnlMain.Visible = false;
            this.SmsQuery();
        }
        private void SmsQuery()
        {
            SqlCommand command = new SqlCommand();
            if (base.Conn.State != ConnectionState.Open)
            {
                base.Conn.Open();
            }
            command.Connection = base.Conn;
            string str = "SELECT [Sms].*,[Users].[UserName],[Users].[Avatar],[Sys_Code].[CodeName] ";
            str = str + " FROM [dbo].[Sms], [dbo].[Users], [dbo].[Sys_Code] " + " WHERE CAST([Sms].[Type] AS nvarchar(20)) = [Sys_Code].[CodeNo] AND [Sys_Code].[CodeFlag] = 'SMS_REMIND' ";
            if (this.txtBeginDate.Value != string.Empty)
            {
                str = str + " AND [Sms].[SendTime] >= @BeginDate ";
                command.Parameters.Add("@BeginDate", Convert.ToDateTime(this.txtBeginDate.Value));
            }
            if (this.txtEndDate.Value != string.Empty)
            {
                str = str + " AND [Sms].[SendTime] <= @EndDate ";
                command.Parameters.Add("@EndDate", Convert.ToDateTime(this.txtEndDate.Value));
            }
            if (this.selectType.Value.ToUpper() == "FROM_ID")
            {
                this.litUserType.Text = "发送人";
                str = str + "AND [Sms].[FromId] = [Users].[UserId] AND [Sms].[ToId] = '" + base.User.Identity.Name + "' ";
                if (this.ToId.Value != string.Empty)
                {
                    str = str + " AND [Sms].[FromId] = @FromId ";
                    command.Parameters.Add("@FromId", this.ToId.Value);
                }
            }
            else
            {
                this.litUserType.Text = "接收人";
                str = str + "AND [Sms].[ToId] = [Users].[UserId] AND [Sms].[FromId] = '" + base.User.Identity.Name + "' ";
                if (this.ToId.Value != string.Empty)
                {
                    str = str + " AND [Sms].[ToId] = @ToId ";
                    command.Parameters.Add("@ToId", this.ToId.Value);
                }
            }
            if (this.selectSeq.Value.ToUpper() == "DESC")
            {
                str = str + " ORDER BY [Sms].[SendTime] DESC";
            }
            else
            {
                str = str + " ORDER BY [Sms].[SendTime] ASC";
            }
            command.CommandText = str;
            SqlDataReader reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                this.pnlQuery.Visible = true;
                this.rptSms.DataSource = reader;
                this.rptSms.DataBind();
            }
            else
            {
                this.pnlQuery.Visible = false;
                this.litMessage.Text = base.ShowPrompt("无符合条件的短信", "query.aspx");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.litNavTitle.Text = "短信查询";
                this.btnSubmit.Attributes.Add("onclick", "return CheckForm();");
                this.btnDelete.Attributes.Add("onclick", "return delete_sms();");
            }
        }


    }
}
